package com.gator.spring.executor.controller;

import com.gator.spring.executor.service.IModifyService;
import com.gator.spring.executor.service.IQueryService;
import com.gator.spring.framework.annotation.PABLO_Autowired;
import com.gator.spring.framework.annotation.PABLO_Controller;
import com.gator.spring.framework.annotation.PABLO_RequestMapping;
import com.gator.spring.framework.annotation.PABLO_RequestParam;
import com.gator.spring.framework.webmvc.servlet.PABLO_ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author PABLO
 * @Date 2022/5/7 13:39
 * @Desc
 */
@PABLO_Controller
@PABLO_RequestMapping("/web")
public class MyTestController {


    @PABLO_Autowired
    IQueryService queryService;
    @PABLO_Autowired
    IModifyService modifyService;

    @PABLO_RequestMapping("/query.json")
    public PABLO_ModelAndView query(HttpServletRequest request, HttpServletResponse response,
                                    @PABLO_RequestParam("name") String name) {
        String result = queryService.query(name);
        return out(response, result);
    }

    @PABLO_RequestMapping("/add*.json")
    public PABLO_ModelAndView add(HttpServletRequest request, HttpServletResponse response,
                                  @PABLO_RequestParam("name") String name, @PABLO_RequestParam("addr") String addr) {
        String result = modifyService.add(name, addr);
        return out(response, result);
    }

    @PABLO_RequestMapping("/remove.json")
    public PABLO_ModelAndView remove(HttpServletRequest request, HttpServletResponse response,
                                     @PABLO_RequestParam("id") Integer id) {
        String result = modifyService.remove(id);
        return out(response, result);
    }

    @PABLO_RequestMapping("/edit.json")
    public PABLO_ModelAndView edit(HttpServletRequest request, HttpServletResponse response,
                                   @PABLO_RequestParam("id") Integer id,
                                   @PABLO_RequestParam("name") String name) {
        String result = modifyService.edit(id, name);
        return out(response, result);
    }


    private PABLO_ModelAndView out(HttpServletResponse resp, String str) {
        try {
            //resp.getWriter().write(str);
            Map<String,String> model = new HashMap<>();
            model.put("teacher","pablo");
            model.put("token","djhsjksmcvnff65f46ds5654xcss");
            model.put("data","何章怀晓");
            PABLO_ModelAndView pablo_modelAndView = new PABLO_ModelAndView("first",model);
            return pablo_modelAndView;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
